import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */'@/views/login.vue'),
    meta: {
      requireAuth: false,
      title: "login"
    }
  },
  {
    path: '/toPay',
    name: 'toPay',
    component: () => import(/* webpackChunkName: "pay" */'@/views/pay/toPay.vue'),
    meta: {
      requireAuth: true,
      title: "toPay"
    }
  },
  {
    path: '/index',
    redirect: '/home',
    component: () => import("@/components/base/index.vue"),
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import("@/views/home.vue"),
        meta: {
          requireAuth: false,
          keepAlive: true,
          title: "首页"
        }
      },

      {
        path: '/issuer',
        name: 'issuer',
        component: () => import("@/views/issuer/issuerList.vue"),
        meta: {
          requireAuth: false,
          keepAlive: true,
          title: "书店区"
        }
      },
      // details
      {
        path: '/details',
        name: 'details',
        component: () => import("@/views/showbooks/bookInfo.vue"),
        meta: {
          requireAuth: false,
          title: "详情"
        }
      },
      {
        path: '/cart',
        name: 'cart',
        component: () => import("@/views/ability/cart.vue"),
        meta: {
          requireAuth: true,
          title: "购物车"
        }
      },
      {
        path: '/filter',
        name: 'filter',
        component: () => import("@/views/showbooks/filter.vue"),
        meta: {
          requireAuth: false,
          title: "筛选"
        }
      },
      {
        path: '/issuerHome',
        name: 'issuerHome',
        component: () => import("@/views/issuer/issuerHome.vue"),
        meta: {
          requireAuth: false,
          keepAlive: false,
          title: "书店主页"
        }
      },
      {
        path: '/personal',
        redirect: '/myHome',
        component: () => import("@/components/personal/personal.vue"),
        redirect: '/myHome',
        children: [
          {
            path: '/myHome',
            name: 'myHome',
            component: () => import("@/views/person/myHome.vue"),
            meta: {
              requireAuth: true,
              title: "我的主页"
            }
          },
          {
            path: '/myInfo',
            name: 'myInfo',
            component: () => import("@/views/person/myInfo.vue"),
            meta: {
              requireAuth: true,
              title: "个人信息"
            }
          },
          {
            path: '/order',
            name: 'order',
            component: () => import("@/views/ability/order.vue"),
            meta: {
              requireAuth: true,
              title: "我的订单"
            }
          },
          {
            path: '/like',
            name: 'like',
            component: () => import("@/views/ability/like.vue"),
            meta: {
              requireAuth: true,
              title: "我的收藏"
            }
          },
          {
            path: '/tv',
            name: 'tv',
            component: () => import("@/views/admin/tv.vue"),
            meta: {
              requireAuth: true,
              title: "公告管理"
            }
          },
          {
            path: '/sellBooks',
            name: 'sellBooks',
            component: () => import("@/views/ability/sellBooks.vue"),
            meta: {
              requireAuth: true,
              title: "我的商品"
            }
          },
          {
            path: '/registerAdmin',
            name: 'registerAdmin',
            component: () => import("@/views/ability/registerAdmin.vue"),
            meta: {
              requireAuth: true,
              title: "注册管理员"
            }
          },
        ]
      }
    ]
  },


]

const router = new VueRouter({
  mode: 'history',
  // 部署路径 文件夹
  // base: process.env.BASE_URL,
  base: process.env.NODE_ENV === "production" ? "/usedbook/" : "/",
  routes
})

// 设置页面的标题
router.afterEach((to) => {
  document.title = to.meta.title;
  // 跳转后自动返回页面顶部
  window.scrollTo(0, 0);
})
//路由重复
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)) {
    console.log("requireAuth")
    if (localStorage.getItem("token")) {
      console.log("token", localStorage.getItem("token"))
      // this.$store.state.user.user.token
      next()
    } else {
      console.log("noToken")
      next({
        path: '/login',
        name: 'login',
        query: {
          refirect: to.fullPath
        }
      })
    }
  }
  else {
    next()
  }
})

export default router
